package cn.tedu.csmall.cart.service.impl;



import cn.tedu.csmall.cart.mapper.CartMapper;
import cn.tedu.csmall.cart.service.ICartService;
import cn.tedu.csmall.commons.exception.CoolSharkServiceException;
import cn.tedu.csmall.commons.pojo.cart.dto.CartAddDTO;
import cn.tedu.csmall.commons.pojo.cart.model.CartTb;
import cn.tedu.csmall.commons.restful.ResponseCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class CartServiceImpl implements ICartService {

    @Autowired
    private CartMapper cartMapper;


    @Override
    public void cartAdd(CartAddDTO cartAddDTO) {
        CartTb cart = new CartTb();
        //通过工具类直接将cartAddDTO对象中的同名属性赋值给cart对象
        BeanUtils.copyProperties(cartAddDTO, cart);
        int row = cartMapper.insertCart(cart);
        if (row == 0){
            throw new CoolSharkServiceException(ResponseCode.INTERNAL_SERVER_ERROR,"操作数据库失败");
        }
        log.info("新增商品到购物车中！{}", cart);
    }

    @Override
    public void deleteCartByUserIdAndCommodityCode(String userId, String commodityCode) {
        int rows = cartMapper.deleteCartByUserIdAndCommodityCode(userId, commodityCode);
        if (rows == 0) {
            throw new CoolSharkServiceException(ResponseCode.BAD_REQUEST, "购物车中没有该商品");
        }
        log.info("删除购物车中的商品！");
    }
}
